package leetcode;

/**
 * 描述:35. 搜索插入位置
 *
 * @author Madison You
 * @created 0:50
 */
public class Easy_35_SearchInsert {
    public static void main(String[] args) {
        int[] arr = {1,3,5,6,7,9,11};
        int i = new Easy_35_SearchInsert().searchInsert(arr, 2);
        System.out.println(i);
    }

    public int searchInsert(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, ans = n;
        while (left <= right) {
            int mid = (right + left) / 2;
            if (target <= nums[mid]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
}
